package com.geovis.emergency.spd.controller.mobile;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillDutyOrgService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillDutyUserService;
import com.geovis.emergency.spd.biz.pc.service.IPcDutyService;
import com.geovis.emergency.spd.cache.UserCache;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDutyOrg;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDutyUser;
import com.geovis.emergency.spd.entity.pc.pojo.vo.MobileDutyUserVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.TreeVO;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：linwl
 * @date ：Created in 2022/6/29 9:41
 * @description：移动端-应急预案指挥模块-值班排班接口
 * @version: v1
 */
@RestController
@RequestMapping("/mobile/pcDuty")
@RequiredArgsConstructor
@Api(value = "移动端-应急预案指挥模块-值班排班接口", tags = "移动端-应急预案指挥模块-值班排班接口")
@Slf4j
@Validated
public class PcDutyMobileController extends BaseController<IPcDutyService> {


    private final IPcDrillDutyUserService pcDrillDutyUserService;
    private final IPcDrillDutyOrgService pcDrillDutyOrgService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "值班编组用户树", notes = "值班编组用户树")
    @PostMapping({"/getDutyUserTree/{drillId}"})
    public Result<List<TreeVO>> getDutyUserTree(@PathVariable("drillId") String drillId){
        return Result.ok(baseService.dutyTree(drillId,null,true));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "值班编组用户", notes = "传演练id")
    @PostMapping("/getDutyUser/{drillId}")
    public Result<List<MobileDutyUserVO>> getDutyUser(@PathVariable("drillId") String drillId) {
        List<PcDrillDutyUser> list = pcDrillDutyUserService.list(
                Wrappers.lambdaQuery(PcDrillDutyUser.class)
                        .isNotNull(PcDrillDutyUser::getPrincipalIds)
                        .eq(PcDrillDutyUser::getPcDrillId, drillId)
        );
        List<MobileDutyUserVO> dutyUserList = Lists.newArrayList();
        for (PcDrillDutyUser pcDrillDutyUser : list) {
            List<String> principalIdList = StrUtil.split(pcDrillDutyUser.getPrincipalIds(), ",");
            List<String> principalNameList = StrUtil.split(pcDrillDutyUser.getPrincipalNames(), ",");
            for (int i = 0; i < principalIdList.size(); i++) {
                MobileDutyUserVO mobileDutyUserVO = BeanUtil.toBean(pcDrillDutyUser, MobileDutyUserVO.class);
                mobileDutyUserVO.setPersonId(principalIdList.get(i));
                mobileDutyUserVO.setPersonName(principalNameList.get(i));
                if (ObjectUtil.isNotNull(pcDrillDutyUser.getPcDrillOrgId())) {
                    PcDrillDutyOrg drillDutyOrg = pcDrillDutyOrgService.getById(pcDrillDutyUser.getPcDrillOrgId());
                    if (ObjectUtil.isNotNull(drillDutyOrg)) {
                        mobileDutyUserVO.setGroupId(drillDutyOrg.getId());
                        mobileDutyUserVO.setGroupName(drillDutyOrg.getOrgName());
                    }
                }
                SystemUser systemUser = UserCache.getUserById(principalIdList.get(i));
                if (ObjectUtil.isNotNull(systemUser)) {
                    mobileDutyUserVO.setUsername(systemUser.getUserName());
                }
                dutyUserList.add(mobileDutyUserVO);
            }
        }
//        List<MobileDutyUserVO> dutyUserList = list.stream().map(drillDutyUser -> {
//            MobileDutyUserVO mobileDutyUserVO = BeanUtil.toBean(drillDutyUser, MobileDutyUserVO.class);
//            mobileDutyUserVO.setPersonId(drillDutyUser.getPrincipalId());
//            mobileDutyUserVO.setPersonName(drillDutyUser.getPrincipalName());
//            if (ObjectUtil.isNotNull(drillDutyUser.getPcDrillOrgId())) {
//                PcDrillDutyOrg drillDutyOrg = pcDrillDutyOrgService.getById(drillDutyUser.getPcDrillOrgId());
//                if (ObjectUtil.isNotNull(drillDutyOrg)) {
//                    mobileDutyUserVO.setGroupId(drillDutyOrg.getId());
//                    mobileDutyUserVO.setGroupName(drillDutyOrg.getOrgName());
//                }
//            }
//            SystemUser systemUser = UserCache.getUserById(drillDutyUser.getPrincipalId());
//            if (ObjectUtil.isNotNull(systemUser)) {
//                mobileDutyUserVO.setUsername(systemUser.getUserName());
//            }
//            return mobileDutyUserVO;
//        }).collect(Collectors.toList());
        return Result.ok(dutyUserList);
    }
}
